package com.hwj.service.Impl;


import com.hwj.entity.Favorite;
import com.hwj.entity.Post;
import com.hwj.mapper.FavoriteMapper;
import com.hwj.mapper.PostMapper;
import com.hwj.service.FavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FavoritesServiceImpl implements FavoritesService {
    @Autowired
    FavoriteMapper favoriteMapper;
    @Autowired
    PostMapper postMapper;


    @Override
    public boolean set(Favorite favorite) {
        Favorite favorite1 = favoriteMapper.selectUserFavorite(favorite);
        Post post = postMapper.selectByPrimaryKey(favorite.getPostId());
        Long count = post.getFavorites();
        if(favorite1==null){
            favoriteMapper.insert(favorite);
            count++;
        }else {
            favoriteMapper.deleteByPrimaryKey(favorite1.getFavoriteId());
            count--;
        }
        post.setFavorites(count);
        postMapper.updateByPrimaryKeySelective(post);
        if(favorite1!=null)
            return false;
        return true;
    }

    @Override
    public boolean flag(Favorite favorite) {
        Favorite Favorite1 = favoriteMapper.selectUserFavorite(favorite);
        if(Favorite1!=null)
            return true;
        return false;
    }
}
